 var actmodel = {
   /**
    *  返回菜单加默认第一个选项
    * @param {*} config 
    */
   getnav(config, clubmode = null, clubmust = -1, ordertype = 1,allowedwh=false) {
     var nav = [{
         id: 0,
         name: "快递配送",
         isshow: false,
         val: 1,
         value: "快递配送"
       },
       {
         id: 1,
         name: "社区团长",
         isshow: false,
         val: 2,
         value: "社区团长"
       },
       {
         id: 2,
         name: "到店自提",
         isshow: false,
         val: 4,
         value: "到店自提"
       },
       {
         id: 3,
         name: "同城配送",
         isshow: false,
         val: 8,
         value: "同城配送"
       },
       {
         id: 4,
         name: "放入仓库",
         isshow: false,
         val: 16,
         value: "放入仓库"
       }
     ];

     var navCur = -1
     var delivery_config = null
     if (config["delivery_config"]) {
       delivery_config = config["delivery_config"]
       if (delivery_config['nav']) {
         nav = delivery_config['nav']
       }
       var mode = 0;
       if (clubmode && clubmust == 1) {
         mode = parseInt(clubmode)
       } else {
         mode = parseInt(delivery_config["mode"])
       }



       nav.forEach(function (item, idx) {
        console.log("ppppppppppppppppp")

        console.log(item)
        console.log(mode)

         if (clubmode && (item.val == clubmode)) {
           navCur = item['id'];
         }
        
          if ((mode & item.val) == item.val) {
            console.log("allowedwh")
            console.log(allowedwh)
            if ((item.val == 16 && ordertype == 17 && allowedwh) || (item.val != 16)) {
              nav[idx]["isshow"] = true
              if (navCur == -1) {
                navCur = item['id'];
              }
            }
 
          } else {
            nav[idx]["isshow"] = false
          }
        
       })



     } else {
       nav[0].isshow = true;
       navCur = 0
     }


     nav = nav.filter(item => {


       return item.isshow == true

     })

     console.log("dddddddddddddddddddddddddddddddddddddddddddddd")
     console.log(nav)

     var paytype = -1
     var allpaymentmethod = [{
       val: 1,
       name: "微信支付"
     }, {
       val: 2,
       name: "余额支付"
     }, {
       val: 4,
       name: "货到付款"
     }, {
       val: 8,
       name: "好友代付"
     }]
     var paymentmethod = []
     allpaymentmethod.forEach(item => {
       if ((item.val & config["payment"].mode) == item.val) {
         if (paytype == -1) {
           paytype = item.val
         }
         if (item.val == 2 && config["payment"]['balancepayname']) {
           item['name'] = config["payment"]['balancepayname'] + '支付'
         }
         paymentmethod.push(item)
       }
     })


     return {
       nav,
       navCur,
       delivery_config,
       paymentmethod,
       paytype
     };
   },
   /**
    * 
    */
   calcsamecity(intracity, distance) {


     var calcsamecityinfo = {};
     var freight = 0


     if (!intracity.start) {
       intracity.start = {
         distance: 0,
         price: 0
       }
     } else {
       if (!intracity.start.distance) {
         intracity.start.distance = 0
       }
       if (!intracity.start.price) {
         intracity.start.price = 0
       }
     }
     if (!intracity.every) {
       intracity.every = {
         distance: 0,
         price: 0
       }
     } else {
       if (!intracity.every.distance) {
         intracity.every.distance = 0
       }
       if (!intracity.every.price) {
         intracity.every.price = 0
       }
     }
     if (!distance) {
       calcsamecityinfo = {
         status: -1,
         message: "请选择地址"
       }
       return {
         calcsamecityinfo,
         freight
       }
     }

     if (distance.val - intracity.maxdistance > 0) {
       calcsamecityinfo = {
         status: -1,
         message: "超出配送范围"
       }
     } else {
       var totalprice = 0
       var nextval = distance.val - intracity.start.distance
       totalprice -= intracity.start.price


       if (nextval > 0) {
         var nextmode = ""
         if (intracity.every.distance == 0) {
           nextmode = 0
         } else {
           nextmode = nextval % intracity.every.distance
         }

         nextval -= nextmode
         if (intracity.every.distance == 0) {
           nextval = 0
         } else {
           nextval = nextval / intracity.every.distance
         }
         if (nextmode > 0) {
           nextval++;
         }
         totalprice -= nextval * intracity.every.price
       }

       freight = totalprice * -100
       calcsamecityinfo = {
         status: 1
       }
     }

     return {
       calcsamecityinfo,
       freight
     }
   },
   /**
    *  计算订单价
    * @param {*} cartdadta 
    * @param {*} freight 
    * @param {*} actcoupon 
    */
   billpriceinfo(cartdadta, freight, actcoupon, rankmoney, packinfo, coindata, ordertype, sysyihuoconfig) {
     var total = actmodel.calculate_orderinfo(cartdadta, packinfo, ordertype, sysyihuoconfig);


     var totalprice = 0; //订单总价
     var pronums = total.pronums; //总数量
     var prototalprice = total.prototalprice; //商品总金额
     var viptotalprice = total.viptotalprice; //vip可折扣总金额
     var packcutprice = total.packcutprice; //vip可折扣总金额
     var clubtotalprice = total.clubtotalprice; //vip可折扣总金额


     var vipcutprice = 0 //vip折扣金额
     if (rankmoney && viptotalprice) {
       var m = (10 - rankmoney) * viptotalprice / 10;
       vipcutprice = m.toFixed(0)
     }


     totalprice = prototalprice - vipcutprice - packcutprice + total.servicecharge
     var cutcoinmoney = 0
     if (coindata) {

       coindata.goldcoin = coindata.goldcoin * 1
       //金币数大于0 且 使用金币
       if (coindata.goldcoin > 0) {
         var maxmoney = coindata.coinmaxrate * totalprice / 100;
         var maxchangeicon = maxmoney * coindata.coinrate / 100

         var needcoin = 0
         if (coindata.goldcoin > maxchangeicon) { //用户金币>最大可使用金币
           needcoin = maxchangeicon
         } else {
           needcoin = coindata.goldcoin
         }
         cutcoinmoney = needcoin / coindata.coinrate * 100
         cutcoinmoney = cutcoinmoney.toFixed(0)


         if (coindata.iconmode == 1) {

           totalprice = totalprice - cutcoinmoney
         }
       }
     }
     var paymoney = totalprice
     if (actcoupon) {
       if (actcoupon.discountmoney >= paymoney) {
         paymoney = 0
       } else {
         paymoney = paymoney - actcoupon.discountmoney;
       }
     }
     var freight = parseInt(freight)
     paymoney += freight
     paymoney = Math.round(paymoney)
    
    

     return {
       prototalprice,
       totalprice,
       pronums,
       paymoney,
       vipcutprice,
       packcutprice,
       cutcoinmoney,
       clubtotalprice,
       servicecharge: total.servicecharge
     }
   },
   calculate_orderinfo(cartdadta, packinfo, ordertype, sysyihuoconfig) {
     var pronums = 0;
     var prototalprice = 0;
     var viptotalprice = 0;
     var packcutprice = 0;
     var clubtotalprice = 0;
     var servicecharge = 0
     if(ordertype==20){
      prototalprice=0;
     }else if(ordertype==21){
      for (var a in cartdadta) {
        var item = cartdadta[a];
        for (var s in item['attrs']) {
          prototalprice += (item['attrs'][s]["realprice"] * 100) * item['attrs'][s]["num"];
          pronums += item['attrs'][s]["num"]
        }
      }
     }else
     if (ordertype == 17) {
       for (var a in cartdadta) {
         var item = cartdadta[a];
         for (var s in item['attrs']) {
           prototalprice += (item['attrs'][s]["actprice"] ? item['attrs'][s]["actprice"] : item['attrs'][s]["wsprice"]) * item['attrs'][s]["num"];
           pronums += item['attrs'][s]["num"]

         }
       }
     } else {
       for (var i in cartdadta) {
         var item = cartdadta[i]
         console.log("item")
         console.log(item)

         var protype = 1
         if (item['arrkval'] && item['arrkval']['4']) {
           protype = item['arrkval']['4']
         }
         if (item['protype']) {
           protype = item['protype']
         }


         pronums += item["num"]
         if (protype == 2) { //团长商品
           clubtotalprice += item["realprice"] * item["num"];
           prototalprice += item["realprice"] * item["num"];
         } else if (protype == 1) { //商城商品
           console.log("xxxxxxxxxx")
           servicecharge += actmodel.makepriceservice(item["realprice"], item["num"], item["customdata"] ? item["customdata"]['yihuo'] : "", sysyihuoconfig)
           switch (item["cid"] * 1) {
             case 0: //普通产品
             {
               viptotalprice += item["realprice"] * item["num"];
               prototalprice += item["realprice"] * item["num"];
               break;
             }
             default: { //活动产品
               prototalprice += item["activityprice"] * item["num"];
               break;
             }
           }
         }

         if (packinfo && packinfo['prokeyinfo'][cartdadta[i]['id']]) {
           if (packinfo['prokeyinfo'][cartdadta[i]['id']]['restnum'] >= cartdadta[i]["num"]) {
             packcutprice += packinfo['prokeyinfo'][cartdadta[i]['id']]['activityprice'] * cartdadta[i]["num"];
           }
         }
       }
     }

     var total = {};
     total['pronums'] = pronums;
     total['prototalprice'] = prototalprice;
     total['viptotalprice'] = viptotalprice;
     total['packcutprice'] = packcutprice;
     total['clubtotalprice'] = clubtotalprice;
     total['servicecharge'] = servicecharge;


     return total
   },

   makepriceservice(realprice, num, yihuoconfig, sysyihuoconfig) {
     var servicecharge = 0;
     if (sysyihuoconfig && sysyihuoconfig['status'] == 1) {
       var type = 1; //1全局 2独立 3无

       if (yihuoconfig && yihuoconfig['servicecharge']) {
         type = yihuoconfig['servicecharge']
       }
       if (type == 1) {
         console.log("type == 1")
         console.log(sysyihuoconfig)
         if (sysyihuoconfig['probuyunit'] == 'fixed') {
           servicecharge = sysyihuoconfig['probuyval'] * 100 * num;
         } else {
           servicecharge = realprice * sysyihuoconfig['probuyval'] * num * 0.01;


         }

       } else if (type == 2) {
         console.log("type == 2")
         console.log(sysyihuoconfig)
         if (yihuoconfig['probuyunit'] == 'fixed') {
           servicecharge = yihuoconfig['probuyval'] * 100 * num;
         } else {
           servicecharge = realprice * yihuoconfig['probuyval'] * num * 0.01;
         }
       }
       servicecharge = servicecharge.toFixed(0)
     }
     console.log("servicecharge")

     console.log(servicecharge)
     return servicecharge * 1;


   },

   billpriceinfo1(cartdadta, freight, actcoupon, vipdata, packinfo = null) {
     if (packinfo) {
       vipdata = null
     }
     var ilen = cartdadta.length;
     var totalprice = 0;
     var pronums = 0;
     var pronums = 0;

     var prototalprice = 0;
     var commomproprice = 0;
     var vipcutprice = 0;
     var packcutprice = 0;

     var freight = parseInt(freight)
     for (var i = 0; i < ilen; i++) {
       pronums += cartdadta[i]["num"];
       commomproprice += ((cartdadta[i]["activityid"] > 0 || cartdadta[i]["activityid"] == '-1001') ? 0 : cartdadta[i].realprice) * cartdadta[i]["num"]
       prototalprice += ((cartdadta[i]["activityid"] > 0 || cartdadta[i]["activityid"] == '-1001') ? cartdadta[i].activityprice : cartdadta[i].realprice) * cartdadta[i]["num"]
       if (packinfo && packinfo['prokeyinfo'][cartdadta[i]['id']]) {
         if (packinfo['prokeyinfo'][cartdadta[i]['id']]['restnum'] >= cartdadta[i]["num"]) {
           packcutprice += packinfo['prokeyinfo'][cartdadta[i]['id']]['activityprice'] * cartdadta[i]["num"];
         }
       }
     }
     if (vipdata && vipdata.rankmoney) {
       var m = (10 - vipdata.rankmoney) * commomproprice / 10;
       vipcutprice = m.toFixed(0)

     }




     totalprice = prototalprice + freight - vipcutprice - packcutprice
     var paymoney = prototalprice - vipcutprice - packcutprice
     if (actcoupon) {
       if (actcoupon.discountmoney >= paymoney) {
         paymoney = 0
       } else {
         paymoney = paymoney - actcoupon.discountmoney;
       }
     }
     paymoney += freight
     paymoney = Math.round(paymoney)
     return {
       prototalprice,
       totalprice,
       pronums,
       paymoney,
       vipcutprice,
       packcutprice
     }
   }
 }
 module.exports = actmodel;